package com.jarvis.app.car.service;

import com.jarvis.app.car.common.MessageResult;
import com.jarvis.app.car.model.Car;
import com.jarvis.app.car.model.User;
import com.jarvis.app.car.util.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class CarService {

    public MessageResult<List<Car>> listAll(Long userId, boolean superUser) {
        List<Car> cars = new ArrayList<>();


        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "";
            if (superUser) {
                sql = "select * from car ";
            } else {
                sql = "select * from car where user_id = ? ";

            }

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            if (!superUser) {
                pst.setLong(1, userId);
            }
            resultSet = pst.executeQuery();

            while (resultSet.next()) {
                long id = resultSet.getLong("id");
                String img = resultSet.getString("img");
                String code = resultSet.getString("code");
                String name = resultSet.getString("name");
                Long userIdDb = resultSet.getLong("user_id");

                Car car = new Car(id, img, code, name, userIdDb);
                cars.add(car);
            }
        } catch (SQLException e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }


        return MessageResult.ok(null, cars);
    }

    public MessageResult<Void> add(Long userId, String carCode, String carName, String carImg) {

        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "insert into car(code,name,img,user_id) values(?,?,?,?) ";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setString(1, carCode);
            pst.setString(2, carName);
            pst.setString(3, carImg);
            pst.setLong(4, userId);
            pst.executeUpdate();


        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }


        return MessageResult.ok();
    }

    public MessageResult<Void> delete(Long userId, Long carId) {
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "delete from  car  where id = ? and user_id = ?";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);

            pst.setLong(1, carId);
            pst.setLong(2, userId);
            int i = pst.executeUpdate();

            if (i > 0) {
                return MessageResult.ok();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
        return MessageResult.fail("非自己车辆不可删除");
    }

    public MessageResult<Car> getOne(long carId) {
        Car car = null;
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "select * from car where id = ? ";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setLong(1, carId);
            resultSet = pst.executeQuery();

            while (resultSet.next()) {
                long id = resultSet.getLong("id");
                String img = resultSet.getString("img");
                String code = resultSet.getString("code");
                String name = resultSet.getString("name");
                Long userIdDb = resultSet.getLong("user_id");

                car = new Car(id, img, code, name, userIdDb);

            }
        } catch (SQLException e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }


        return MessageResult.ok(null, car);
    }
}
